﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcs.Web.Admin.gradeinfo
{
    public partial class MyList : lcs.Web.Admin.BasePage
    {
        lcs.BLL.gradeinfo bll = new lcs.BLL.gradeinfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page.DataBind();
                BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }


        #region gridView

        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion

            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and sname like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtKeyword2.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and projectname like '%{0}%'", txtKeyword2.Text.Trim());
            }
            if (txtwinbid.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and purchaseid like '%{0}%'", txtwinbid.Text.Trim());
            }
            ds = bll.GetList(strWhere.ToString());
            gridView.DataSource = ds;
            gridView.DataBind();
            int id = getroleid();
            //if (id == 3)
            //{
            //    gridView.Columns[31].Visible = true;
            //}
            //if (id == 3)
            //{
            //    gridView.Columns[31].Visible = true;
            //}
            //if (id == 3)
            //{
            //    gridView.Columns[31].Visible = true;
            //}
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void btnOut_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();

            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and sname like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtKeyword2.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and projectname like '%{0}%'", txtKeyword2.Text.Trim());
            }
            if (txtwinbid.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and purchaseid like '%{0}%'", txtwinbid.Text.Trim());
            }
            ds = bll.GetList(strWhere.ToString());
            string err = "";
            OutExcel.ExportToExcel(this.Response, ds.Tables[0], DateTime.Now.Ticks.ToString() + ".xls",  ref err);
            //DateTime dt = System.DateTime.Now;
            //string str = dt.ToString("yyyyMMddhhmmss");
            //str = str + ".xls";
            //gridView.AllowPaging = false;
            ////以下为要隐藏的列
            ////gridView.Columns[0].Visible = false;
            ////gridView.Columns[12].Visible = false;
            //BindData();
            //GridViewToExcel(gridView, "application/ms-excel", str);
            //gridView.AllowPaging = true;
            ////gridView.Columns[0].Visible = true;
            ////gridView.Columns[12].Visible = true;
            //BindData();

        }
        public static void GridViewToExcel(Control ctrl, string FileType, string FileName)
        {
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
            HttpContext.Current.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
            HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 
            ctrl.Page.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            ctrl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                //linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}

            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

    }
}